package control_centre

import (
	"fmt"
	"github.com/hajimehoshi/ebiten/v2"
	"study/PlantsVsZombies1/utils"
	"time"
)

func BackGroundDraw(screen *ebiten.Image) {

	// 场景的绘制
	op := &ebiten.DrawImageOptions{}
	op.GeoM.Translate(-200, 0)
	if background1, ok := Images["background1"]; ok {
		screen.DrawImage(background1, op)
	}

}

// --------------------植物模块-----------------------

func CardSlotDraw(screen *ebiten.Image) {
	// 卡槽绘制
	op := &ebiten.DrawImageOptions{}
	op.GeoM.Translate(20, 0)
	if cardSlot, ok := Images["card_slot"]; ok {
		screen.DrawImage(cardSlot, op)
	}
}

func PlantCardDraw(screen *ebiten.Image) {
	var distance = 87.0
	// 植物绘制

	for i := 0; i < len(PlantCardSlice); i++ {
		op := &ebiten.DrawImageOptions{}
		name := PlantCardSlice[i]
		distance = distance + 4
		op.GeoM.Scale(0.82, 0.82)
		op.GeoM.Translate(distance, 5.4)
		if plant, ok := Images[name]; ok {
			screen.DrawImage(plant, op)
		}
		distance = distance + 52

	}

}

func (g *Game) GrowPlant(screen *ebiten.Image) {
	name := utils.GetClickedPlantCard(PlantCardCoordination)
	//var TemporaryName string

	if g.MouseState == ClickTheMouse && name != "" && g.DragPlantCardState == Deactivation {
		g.DragPlantCardState = Activate
		MouseBindPlantImagePath = utils.MouseBindPlantSource(name)
		g.staticName.GrowPlantTemporaryName = name

		fmt.Println(name)
	}
	if g.DragPlantCardState == Activate && g.MouseState == ReleaseTheMouse {

		//ToDO implement me 填写游戏的种植逻辑
		i, j := utils.GetTurfGrid(utils.GetCoordination())
		// 为map数组赋值的方法
		if i >= 0 && i < 5 && j >= 0 && j < 9 {

			// 更改键值
			turf := TurfGrid[i][j][0]
			turf.state = 1
			turf.Image = GrowPlantImageSlice[g.staticName.GrowPlantTemporaryName] // 传递动态植物图片
			turf.NamePlant = g.staticName.GrowPlantTemporaryName                  // 传递植物姓名
			turf.PlantHp = utils.GetPlantHP(turf.NamePlant)                       // 设置植物血量
			if turf.NamePlant == "nutWall" {
				nutWallHp = turf.PlantHp
			}
			turf.LinePosition = i                                                                   // 传递行数
			turf.PlantAttackFrequency = g.PlantAttackFrequency(g.staticName.GrowPlantTemporaryName) // 攻击速度
			turf.lastTime = time.Now()

			// 赋值
			TurfGrid[i][j][0] = turf
			fmt.Println("该植物共有", len(turf.Image), "张状态图")
			fmt.Println("该草坪状态", TurfGrid[i][j][0].state, g.staticName.GrowPlantTemporaryName)

		} else {
			fmt.Println("种值不规范")
		}

		g.DragPlantCardState = Deactivation

	} else if g.DragPlantCardState == Activate && g.MouseState == DragTheMouse {
		x, y := ebiten.CursorPosition()
		if MouseBindPlantImagePath != nil {
			op := &ebiten.DrawImageOptions{}
			op.GeoM.Translate(float64(x)-40, float64(y)-40)
			screen.DrawImage(MouseBindPlantImagePath, op)
		}
	}
}

// CalcTurfCoordinates 计算草坪坐标
func CalcTurfCoordinates() {
	// 初始化，草坪，为种植植物做准备
	for i := range TurfGrid {
		for j := range TurfGrid[i] {

			// 首相初始化，每个map,分配空间
			TurfGrid[i][j] = make(map[int]struct {
				state             int // 0:空 1:植物 2:僵尸
				Top               float64
				Bottom            float64
				Right             float64
				Left              float64
				SwingTimeInterval int
				lastTime          time.Time
				Image             []*ebiten.Image

				NamePlant            string         // 植物姓名
				PlantHp              int            // 植物血量
				LinePosition         int            // 植物所处行距
				PlantSituation       PlantSituation // 植物状态
				PlantAttackFrequency int            // 植物攻击频率
				PlantLastTimeAttack  time.Time      // 植物上一次攻击的时间

				CherryBombDisplayCount int // 樱桃炸弹存在次数
			})

			// 开始传值
			TurfGrid[i][j][0] = struct {
				state             int
				Top               float64
				Bottom            float64
				Right             float64
				Left              float64
				SwingTimeInterval int
				lastTime          time.Time
				Image             []*ebiten.Image

				NamePlant            string         // 植物姓名
				PlantHp              int            // 植物血量
				LinePosition         int            // 植物所处行距
				PlantSituation       PlantSituation // 植物状态
				PlantAttackFrequency int            // 植物攻击频率
				PlantLastTimeAttack  time.Time      // 植物上一次攻击的时间

				CherryBombDisplayCount int // 樱桃炸弹存在次数
			}{
				state:             0,
				Top:               float64(100*i + 75),
				Bottom:            float64(100*(i+1) + 75),
				Right:             float64(80*(j+1) + 50),
				Left:              float64(80*j + 50),
				SwingTimeInterval: 0,
				lastTime:          time.Now(),
				Image:             nil,

				NamePlant:            "",         // 植物姓名
				PlantHp:              0,          // 植物血量
				LinePosition:         -1,         // 植物所处行距
				PlantSituation:       secure,     // 植物状态
				PlantAttackFrequency: 0,          // 植物攻击频率
				PlantLastTimeAttack:  time.Now(), // 植物上一次攻击的时间

				CherryBombDisplayCount: 0, // 樱桃炸弹存在次数
			}
		}

	}

	fmt.Println("各块草坪坐标加载完毕")
	//for i := range TurfGrid {
	//	for j := range TurfGrid[i] {
	//
	//		// 开始传值
	//		fmt.Println(i, "  ", j)
	//		fmt.Println(TurfGrid[i][j][0].Top)
	//		fmt.Println(TurfGrid[i][j][0].Bottom)
	//		fmt.Println(TurfGrid[i][j][0].Right)
	//		fmt.Println(TurfGrid[i][j][0].Left)
	//
	//	}
	//}

}

// DisplayPlantImage 绘制植物动态图
func (g *Game) DisplayPlantImage(screen *ebiten.Image) {

	for i := range TurfGrid {
		for j := range TurfGrid[i] {
			if TurfGrid[i][j][0].state == 1 { // 该草坪内存在植物
				//&& TurfGrid[i][j][0].PlantHp >= 0
				if TurfGrid[i][j][0].NamePlant == "cherryBomb" { // 该草坪内的植物是，樱桃炸弹时
					// 确保 Image 切片有元素
					if len(TurfGrid[i][j][0].Image) > 0 {
						// 创建结构体，并被赋值
						turfGridStruct := TurfGrid[i][j][0]

						// 时间值计算
						turfGridStruct.SwingTimeInterval = utils.CalTimeDiff(turfGridStruct.lastTime, time.Now())
						interval := (turfGridStruct.SwingTimeInterval) / 120

						// 在这里也要更新，否则会出现闪屏
						op := &ebiten.DrawImageOptions{}

						if interval < len(turfGridStruct.Image) {
							//fmt.Println("出现樱桃1111111111111111111111111111111")
							op.GeoM.Translate(TurfGrid[i][j][0].Left+10, TurfGrid[i][j][0].Top+20)
							screen.DrawImage(TurfGrid[i][j][0].Image[interval], op)
						} else if interval >= len(turfGridStruct.Image) {
							// Todo implement me
							// 达到临界值，则重置问题数值
							// 展示爆炸后的图片
							turfGridStruct.CherryBombDisplayCount++

							if turfGridStruct.CherryBombDisplayCount <= 24 {
								op.GeoM.Translate(TurfGrid[i][j][0].Left-30, TurfGrid[i][j][0].Top-30)
								screen.DrawImage(cherryBombLastImage, op)
							}

							// 进行范围判定
							ScanZombieToDead(turfGridStruct.Top, turfGridStruct.Left, turfGridStruct.Bottom, turfGridStruct.Right)

							// 删除樱桃炸弹，即所有清零
							if turfGridStruct.CherryBombDisplayCount >= 24 {
								turfGridStruct.state = 0
								turfGridStruct.SwingTimeInterval = 0
								turfGridStruct.Image = nil

								turfGridStruct.NamePlant = ""
								turfGridStruct.PlantHp = 0
								turfGridStruct.LinePosition = -1
								turfGridStruct.PlantSituation = secure
								turfGridStruct.PlantAttackFrequency = 0
								turfGridStruct.PlantLastTimeAttack = time.Now()
								turfGridStruct.CherryBombDisplayCount = 0
							}

						}
						// 返回修改后的结构体
						TurfGrid[i][j][0] = turfGridStruct
					} else {
						// 处理 Image 为空的情况
						fmt.Println("No image available for position:", i, j)
					}

				} else if TurfGrid[i][j][0].state == 1 && TurfGrid[i][j][0].NamePlant == "nutWall" {
					if len(TurfGrid[i][j][0].Image) > 0 {
						// Todo implement me
						// 创建结构体，并被赋值
						turfGridStruct := TurfGrid[i][j][0]

						op := &ebiten.DrawImageOptions{}

						if turfGridStruct.PlantHp <= nutWallHp/3*2 && turfGridStruct.PlantHp >= nutWallHp/3 {
							op.GeoM.Translate(TurfGrid[i][j][0].Left+10, TurfGrid[i][j][0].Top+20)
							screen.DrawImage(NutWallStateImage[0], op)
						} else if turfGridStruct.PlantHp <= nutWallHp/3 && turfGridStruct.PlantHp >= 0 {
							op.GeoM.Translate(TurfGrid[i][j][0].Left+10, TurfGrid[i][j][0].Top+30)
							screen.DrawImage(NutWallStateImage[1], op)
						} else if turfGridStruct.PlantHp >= nutWallHp/3*2 {
							op.GeoM.Translate(TurfGrid[i][j][0].Left+10, TurfGrid[i][j][0].Top+20)
							screen.DrawImage(TurfGrid[i][j][0].Image[0], op)
						} else if turfGridStruct.PlantHp < 0 {
							turfGridStruct.state = 0
							turfGridStruct.SwingTimeInterval = 0
							turfGridStruct.Image = nil

							turfGridStruct.NamePlant = ""
							turfGridStruct.PlantHp = 0
							turfGridStruct.LinePosition = -1
							turfGridStruct.PlantSituation = secure
							turfGridStruct.PlantAttackFrequency = 0
							turfGridStruct.PlantLastTimeAttack = time.Now()
							turfGridStruct.CherryBombDisplayCount = 0
						}
						// 返回修改后的结构体
						TurfGrid[i][j][0] = turfGridStruct
					} else {
						// 处理 Image 为空的情况
						fmt.Println("No image available for position:", i, j)
					}

				} else { // 种植其他植物的可能
					// 确保 Image 切片有元素
					if len(TurfGrid[i][j][0].Image) > 0 {
						// Todo implement me
						// 创建结构体，并被赋值
						turfGridStruct := TurfGrid[i][j][0]

						// 时间值计算
						turfGridStruct.SwingTimeInterval = utils.CalTimeDiff(turfGridStruct.lastTime, time.Now())
						interval := (turfGridStruct.SwingTimeInterval) / 120

						// 在这里也要更新，否则会出现闪屏
						op := &ebiten.DrawImageOptions{}
						op.GeoM.Translate(TurfGrid[i][j][0].Left+10, TurfGrid[i][j][0].Top+20)
						if interval < len(turfGridStruct.Image) {
							screen.DrawImage(TurfGrid[i][j][0].Image[interval], op)
						} else if interval >= len(turfGridStruct.Image) {
							// 达到临界值，则重置问题数值
							turfGridStruct.SwingTimeInterval = 0
							turfGridStruct.lastTime = time.Now()
							screen.DrawImage(TurfGrid[i][j][0].Image[0], op)
						}
						if turfGridStruct.PlantHp < 0 {
							turfGridStruct.state = 0
							turfGridStruct.SwingTimeInterval = 0
							turfGridStruct.Image = nil

							turfGridStruct.NamePlant = ""
							turfGridStruct.PlantHp = 0
							turfGridStruct.LinePosition = -1
							turfGridStruct.PlantSituation = secure
							turfGridStruct.PlantAttackFrequency = 0
							turfGridStruct.PlantLastTimeAttack = time.Now()
							turfGridStruct.CherryBombDisplayCount = 0
						}

						// 返回修改后的结构体
						TurfGrid[i][j][0] = turfGridStruct
					} else {
						// 处理 Image 为空的情况
						fmt.Println("No image available for position:", i, j)
					}
				}

			}
		}
	}

}

// UpdatePlantState 遍历植物集合
func UpdatePlantState(PlantExistence *[5]PlantSituation) {
	// 遍历植物集合
	for i := range TurfGrid {
		for j := range TurfGrid[i] {
			// 更改植物状态
			plantMap := TurfGrid[i][j][0]

			if plantMap.LinePosition != -1 && ZombieExistence[plantMap.LinePosition] == Danger && plantMap.PlantSituation == secure {
				plantMap.PlantSituation = Danger

				//fmt.Println("第一行", plantMap.PlantSituation, "第二行", plantMap.PlantSituation, "第三行", plantMap.PlantSituation, "第四行", plantMap.PlantSituation, "第五行", plantMap.PlantSituation)

			} else if plantMap.LinePosition != -1 && ZombieExistence[plantMap.LinePosition] == secure && plantMap.PlantSituation == Danger {
				plantMap.PlantSituation = secure
				//fmt.Println("第一行", plantMap.PlantSituation, "第二行", plantMap.PlantSituation, "第三行", plantMap.PlantSituation, "第四行", plantMap.PlantSituation, "第五行", plantMap.PlantSituation)

			}

			TurfGrid[i][j][0] = plantMap
		}
	}
	//plantMap := TurfGrid[0][0][0]
	//plantMap1 := TurfGrid[1][0][0]
	//plantMap2 := TurfGrid[2][0][0]
	//plantMap3 := TurfGrid[3][0][0]
	//plantMap4 := TurfGrid[4][0][0]
	//fmt.Println("第一行", plantMap.PlantSituation, "第二行", plantMap1.PlantSituation, "第三行", plantMap2.PlantSituation, "第四行", plantMap3.PlantSituation, "第五行", plantMap4.PlantSituation)
}

// ShootPea 发射豌豆子弹 -- 主要用于创建子弹
func ShootPea() {
	// 遍历植物集合
	for i := range TurfGrid {
		for j := range TurfGrid[i] {
			// 更改植物状态
			plantMap := TurfGrid[i][j][0]
			AttackTime := utils.CalTimeDiff(plantMap.PlantLastTimeAttack, time.Now())
			//fmt.Println(plantMap.PlantSituation, "    ", plantMap.NamePlant, "    ", plantMap.PlantAttackFrequency)
			// 检测植物是否为危险状态 --- 检测是否为豌豆 -- 开始发射豌豆
			if plantMap.PlantSituation == Danger && plantMap.NamePlant == "peashooter" && AttackTime/20 >= plantMap.PlantAttackFrequency {
				// 创建子弹结构体
				PeaShoot := Pea{
					left:              plantMap.Left + 45,
					top:               plantMap.Top + 20,
					bottom:            plantMap.Top + 44,
					right:             plantMap.Left + 45 + 24,
					speed:             float64(plantMap.PlantAttackFrequency) / 40,
					state:             NoCollision,
					displayNum:        8, // 豌豆展示的次数
					peaFirstCollision: 0,
				}
				// 将此结构体存入
				peaAll = append(peaAll, PeaShoot)

				if AttackTime/20 >= plantMap.PlantAttackFrequency {
					plantMap.PlantLastTimeAttack = time.Now()
				}
			}

			TurfGrid[i][j][0] = plantMap
		}
	}
}

// DrawPeaAttack 遍历所有豌豆
func DrawPeaAttack(screen *ebiten.Image) {

	// 绘制
	for i := range peaAll {
		if peaAll[i].state == NoCollision {
			pea := peaAll[i]
			op := &ebiten.DrawImageOptions{}
			op.GeoM.Translate(pea.left, pea.top)
			screen.DrawImage(peaImage[0], op)
			pea.left += pea.speed
			peaAll[i] = pea

		} else if peaAll[i].state == Collision {
			peaAll[i].displayNum--
			pea := peaAll[i]
			op := &ebiten.DrawImageOptions{}
			op.GeoM.Translate(pea.left+40, pea.top)
			screen.DrawImage(peaImage[1], op)
		}

	}

}

// DeletePeaOverWall 删除豌豆
func DeletePeaOverWall() {
	for i := len(peaAll) - 1; i >= 0; i-- {
		if peaAll[i].left > 1000 || (peaAll[i].state == Collision && peaAll[i].displayNum <= 0) {
			peaAll = append(peaAll[:i], peaAll[i+1:]...) // i之前，与i+1之后
		}
	}
}

// PlantAttackFrequency 返回豌豆的速度
func (g *Game) PlantAttackFrequency(name string) int {
	if name == "peashooter" {
		return g.Config.PeaShooterAttackFrequency
	}
	return 0
}

// DetectPlantDead 检测植物是否死亡
func DetectPlantDead() {

	for i := range TurfGrid {
		for j := range TurfGrid[i] { // 遍历植物
			turfGridStruct := TurfGrid[i][j][0]
			if turfGridStruct.NamePlant == "nutWall" {
				if turfGridStruct.PlantHp <= 2000 && turfGridStruct.PlantHp >= 1000 {

				}
			} else if turfGridStruct.PlantHp <= 0 { // 所有清空，重置
				turfGridStruct.state = 0
				turfGridStruct.SwingTimeInterval = 0
				turfGridStruct.Image = nil
				turfGridStruct.lastTime = time.Now()
				turfGridStruct.NamePlant = ""
				turfGridStruct.PlantHp = 0
				turfGridStruct.LinePosition = -1
				turfGridStruct.PlantSituation = secure
				turfGridStruct.PlantAttackFrequency = 0
				turfGridStruct.PlantLastTimeAttack = time.Now()
				turfGridStruct.CherryBombDisplayCount = 0
			}

		}
	}
}

//
// ------------------僵尸模块-----------------------

// InitZombie 初始化，僵尸批量制作制作
func InitZombie(cfg *Config) {
	for i := 0; i < cfg.ZombieNumbers; i++ {
		var yn = utils.GetRandomNumber(5)
		var ZombieEnterTime = utils.GetRandomNumber(100000) // 10s内季节完毕，如果想增加时间，直接在后方加100000

		zombieMap := map[string]ZombieDate{} // 直接初始化空间

		zombieMap["Zombie"] = ZombieDate{
			Top:                    float64(75 - 44 + yn*100), // n为第几格
			Bottom:                 float64(75 - 44 + (yn+1)*100),
			Right:                  986,
			Left:                   820, // 820
			speed:                  cfg.ZombieSpeed,
			EnterTheArenaTime:      ZombieEnterTime,
			LinePosition:           yn, // 0-4随机
			ZombieHP:               12,
			ZombieState:            live, // 0:活着 1：死亡
			ZombieCountdownToDeath: time.Now(),
			ZombieEat:              NoEat, // 僵尸eat状态
		}
		zombie = append(zombie, zombieMap)
	}
	fmt.Println("僵尸部队已到位")
}

// DisplayZombieImage 绘制僵尸
func (g *Game) DisplayZombieImage(screen *ebiten.Image) {
	for i := 0; i < 5; i++ {
		CorrectRenderZombie(i, screen)
	}
}

// CorrectRenderZombie 绘制僵尸 -- 拓展
func CorrectRenderZombie(n int, screen *ebiten.Image) {
	var flag = true

	for i := len(zombie) - 1; i >= 0; i-- { // 用--，为了更的遍历删除
		zombieMap := zombie[i]["Zombie"]

		if zombieMap.ZombieState == live && zombieMap.ZombieEat == NoEat {
			if zombieMap.LinePosition == n {

				op := &ebiten.DrawImageOptions{}
				op.GeoM.Translate(zombieMap.Left, zombieMap.Top)
				timeDiff := utils.CalTimeDiff(EnterTheArenaPreheatTime, time.Now())
				timeDiffToggle := utils.CalTimeDiff(ZombieToggleMovement, time.Now())
				if timeDiff%2 == 0 && timeDiff >= zombieMap.EnterTheArenaTime { // ------10------ 测试断点

					zombieMap.Left -= zombieMap.speed
					zombieMap.Right -= zombieMap.speed
					// fmt.Println(timeDiff) // 时间差计算

				}

				if ToggleState := timeDiffToggle / 100; ToggleState <= 21 {
					screen.DrawImage(ZombieSlice[ToggleState], op)
				} else {
					ZombieToggleMovement = time.Now()
				}

				if zombieMap.Left < -100 { // 调节--僵尸循环--算法
					zombieMap.Left = 986
					zombieMap.Right = 1160

				}

			}
		} else if zombieMap.ZombieState == Dead && zombieMap.ZombieEat == NoEat {
			if zombieMap.LinePosition == n {

				op := &ebiten.DrawImageOptions{}
				op.GeoM.Translate(zombieMap.Left, zombieMap.Top)
				timeDiff := utils.CalTimeDiff(zombieMap.ZombieCountdownToDeath, time.Now())

				count := timeDiff / 100
				if count >= 0 && count < 19 {
					zombieMap.Left -= zombieMap.speed / 4
					zombieMap.Right -= zombieMap.speed / 4
					screen.DrawImage(ZombieDead[count], op)
				} else if count >= 19 && count < len(ZombieDead) {
					screen.DrawImage(ZombieDead[count], op)
				} else if count >= len(ZombieDead) {
					flag = false
					// 删除
					zombie = append(zombie[:i], zombie[i+1:]...)
				}

			}
		} else if zombieMap.ZombieState == BombDead { // 僵尸爆炸
			if zombieMap.LinePosition == n {

				op := &ebiten.DrawImageOptions{}
				op.GeoM.Translate(zombieMap.Left+50, zombieMap.Top+30)
				timeDiff := utils.CalTimeDiff(zombieMap.ZombieCountdownToDeath, time.Now())

				count := timeDiff / 100
				if count >= 0 && count < 10 {
					screen.DrawImage(ZombieBombImage, op)
					fmt.Println("僵尸成灰了")
				} else if count >= 10 {
					flag = false
					// 删除
					zombie = append(zombie[:i], zombie[i+1:]...)
				}

			}
		} else if zombieMap.ZombieEat == IsEat && zombieMap.ZombieState == live {
			if zombieMap.LinePosition == n {

				op := &ebiten.DrawImageOptions{}
				op.GeoM.Translate(zombieMap.Left, zombieMap.Top)

				timeDiffToggle := utils.CalTimeDiff(ZombieToggleMovement, time.Now())

				if ToggleState := timeDiffToggle / 100; ToggleState <= 20 {
					screen.DrawImage(ZombieEating[ToggleState], op)
				} else {
					ZombieToggleMovement = time.Now()
				}

			}
		} else if zombieMap.ZombieEat == IsEat && zombieMap.ZombieState == Dead {
			if zombieMap.LinePosition == n {
				op := &ebiten.DrawImageOptions{}
				op.GeoM.Translate(zombieMap.Left, zombieMap.Top)
				timeDiff := utils.CalTimeDiff(zombieMap.ZombieCountdownToDeath, time.Now())

				count := timeDiff / 100
				if count >= 0 && count < 10 {
					//zombieMap.Left -= zombieMap.speed / 4
					//zombieMap.Right -= zombieMap.speed / 4
					screen.DrawImage(ZombieEatDead[count], op)
				} else if count >= len(ZombieEatDead) {
					flag = false
					// 删除
					zombie = append(zombie[:i], zombie[i+1:]...)
				}
			}
		}
		// 回归
		if flag {
			zombie[i]["Zombie"] = zombieMap
		}

	}
}

// ScanLawnZombie 检测草坪内的僵尸
func ScanLawnZombie(ZombieExistence *[5]PlantSituation) {
	// 为了方便刷新，现在重置一次
	for i := range ZombieExistence {
		ZombieExistence[i] = secure
	}

	// 检测僵尸是否存在
	for i := range zombie { // 遍历僵尸
		zombieMap := zombie[i]["Zombie"]
		if zombieMap.Left >= -60 && zombieMap.Left <= 700 { // 僵尸一出现，就被激活
			ZombieExistence[zombieMap.LinePosition] = Danger
		}
	}
	// 大型模拟测试
	//fmt.Println("第一行", ZombieExistence[0], "第二行", ZombieExistence[1], "第三行", ZombieExistence[2], "第四行", ZombieExistence[3], "第五行", ZombieExistence[4])

}

func PrintlnZombieScan(ZombieExistence *[5]PlantSituation) { // 测试，是否出现僵尸
	for i := range ZombieExistence {
		fmt.Println("第", i+1, "行：", ZombieExistence[i])
	}
}

// CollisionZombie 豌豆与僵尸
func CollisionZombie() {

	for i := range peaAll {
		for j := range zombie {
			if PeaCollision(peaAll[i].top, peaAll[i].left, peaAll[i].bottom, peaAll[i].right, zombie[j]["Zombie"].Top, zombie[j]["Zombie"].Left+40, zombie[j]["Zombie"].Bottom, zombie[j]["Zombie"].Right) {
				peaAll[i].state = Collision
				// Todo Implement me
				if peaAll[i].state == Collision && peaAll[i].peaFirstCollision == 0 {

					hp := zombie[j]["Zombie"]
					hp.ZombieHP--
					zombie[j]["Zombie"] = hp

					fmt.Println("血量显示", zombie[j]["Zombie"].ZombieHP)
				}
				peaAll[i].peaFirstCollision++

				// 检测判断
				fmt.Println("发生碰撞")
			}
		}
	}
}

// PeaCollision 碰撞检测
func PeaCollision(top1, left1, bottom1, right1, top2, left2, bottom2, right2 float64) bool {
	if top1 > top2 && left1 > left2 && bottom1 < bottom2 && right1 < right2 {

		return true
	}
	return false
}

// ScanZombieDead 遍历检测僵尸是否死亡
func ScanZombieDead() {
	for i := range zombie {
		for j := range zombie[i] {
			if zombie[i][j].ZombieHP <= 0 && zombie[i][j].ZombieState == live {
				State := zombie[i][j]

				State.ZombieState = Dead
				State.ZombieCountdownToDeath = time.Now()

				zombie[i][j] = State
			}
		}
	}
}

// ScanZombieToDead 僵尸爆炸
func ScanZombieToDead(Top, Left, Bottom, Right float64) {
	// 爆炸范围
	var (
		top    = Top - 150
		left   = Left - 170
		right  = Right + 170
		bottom = Bottom + 150
	)

	// 寻找僵尸，并赋予状态
	for i := range zombie {
		zombieBomb := zombie[i]["Zombie"]
		if zombieBomb.ZombieState == live || zombieBomb.ZombieState == Dead {
			if top < zombieBomb.Top && left < zombieBomb.Left && right > zombieBomb.Right && bottom > zombieBomb.Bottom {

				zombieBomb.ZombieState = BombDead
				zombieBomb.ZombieCountdownToDeath = time.Now()
				zombie[i]["Zombie"] = zombieBomb

				fmt.Println("僵尸死了", zombieBomb.ZombieState)
			}
		}
	}
}

// ScanZombiePlantCollision 僵尸与植物是否接触
func ScanZombiePlantCollision() {

	for i1 := range TurfGrid {
		for i3 := range TurfGrid[i1] { // 遍历所有草坪

			turfGrid := TurfGrid[i1][i3][0]

			if turfGrid.NamePlant != "" { // 该草坪存在植物

				for i := len(zombie) - 1; i >= 0; i-- { // 遍历僵尸
					flag := ZombieEat(turfGrid.Top, turfGrid.Left, turfGrid.Bottom, turfGrid.Right, zombie[i]["Zombie"])
					if flag {
						zombieInfo := zombie[i]["Zombie"] // 抽出
						zombieInfo.ZombieEat = IsEat      // 更改
						turfGrid.PlantHp--
						TurfGrid[i1][i3][0] = turfGrid
						fmt.Println(TurfGrid[i1][i3][0].PlantHp)
						//fmt.Println("僵尸正在吃")
						zombie[i]["Zombie"] = zombieInfo // 放回
					}
				}

			} else {
				for i := len(zombie) - 1; i >= 0; i-- { // 遍历僵尸
					flag := ZombieEat(turfGrid.Top, turfGrid.Left, turfGrid.Bottom, turfGrid.Right, zombie[i]["Zombie"])
					if zombie[i]["Zombie"].ZombieEat == IsEat && flag {
						zombieInfo := zombie[i]["Zombie"] // 抽出
						zombieInfo.ZombieEat = NoEat      // 更改
						//fmt.Println("僵尸不吃了")
						zombie[i]["Zombie"] = zombieInfo // 放回
					}
				}
			}

			//fmt.Println(TurfGrid[i1][i3][0].PlantHp)
		}
	}

}

// ZombieEat 僵尸是否在吃植物
func ZombieEat(top1, left1, bottom1, right1 float64, date ZombieDate) bool {
	var (
		top2    = date.Top + 40
		left2   = date.Left
		right2  = date.Right
		bottom2 = date.Bottom + 50
	)
	if top1 > top2 && left1 > left2 && bottom1 < bottom2 && right1 < right2 {
		return true
	} else {
		return false
	}
}
